<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2020/3/11
 * Time: 14:30
 */

namespace app\admin\controller;


use think\Db;
use think\Request;

class Notice extends Base
{
    /**
     * 消息列表
     * @return mixed|\think\response\Json
     */
    public function index(){
        if(request()->isAjax()){
            $keyword = input('keyword');
            $start = input('start');
            $end = input('end');
            $where = [];
            if(isset($keyword) && $keyword != "")
            {
                $where['notice_title'] = ['like', "%" . $keyword . "%"];
            }
            if(isset($start) && $start != "" && isset($end) && $end == "")
            {
                $where['notice_time'] = ['>= time', $start];
            }
            if(isset($end) && $end != "" && isset($start) && $start == "")
            {
                $where['notice_time'] = ['<= time', $end];
            }
            if(isset($start) && $start != "" && isset($end) && $end != "")
            {
                $where['notice_time'] = ['between time', [$start, $end]];
            }
            $NowPage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('notice')->where($where)->count();// 获取总条数
            $lists = Db::name('notice')
                ->where($where)
                ->field('notice_id, notice_title, notice_time')
                ->page($NowPage,$limits)
                ->order('notice_time desc')
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }
    
    /**
     * 添加消息
     * @return mixed|\think\response\Json
     */
    public function add(){
        if(Request::instance()->isPost()){
            $notice_title = trim(input('notice_title'), ' ');
            $notice_content = input('notice_content');
            $insert = [
                'notice_title' => $notice_title,
                'notice_content' => $notice_content,
                'notice_receive_user_id' => -1,
                'notice_read_user_id' => ',0,',
                'notice_del_user_id' => ',0,',
                'notice_time' => date('Y-m-d H:i:s')
            ];
            $res = Db::table('up_notice')->insert($insert);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        return $this->fetch();
    }
    
    /**
     * 消息详情
     * @return mixed
     */
    public function detail(){
        $notice_id = input('notice_id');
        $detail = Db::table('up_notice')
            ->field('notice_title, notice_content')
            ->where('notice_id', $notice_id)
            ->find();
        $this->assign('detail', $detail);
        return $this->fetch();
    }
    
    /**
     * 删除消息
     * @return \think\response\Json
     */
    public function del(){
        $notice_id = input('id');
        $res = Db::table('up_notice')->delete($notice_id);
        if($res){
            return json(['code' => 200, 'msg' => '删除成功']);
        }else{
            return json(['code' => 100, 'msg' => '删除失败']);
        }
    }
}